<?php

    class SQLController{
    
        private $database;

        private function sql_connect()
        {
            global $mysql_server;
            global $mysql_username;
            global $mysql_password;
            global $mysql_database;

            $link = @mysql_connect($mysql_server,$mysql_username,$mysql_password) or $this->Mysql_Die(mysql_error());
            @mysql_query("SET NAMES utf8");
            @mysql_select_db($mysql_database);
            return $link;
        }

        public function query($query,$debug=false)
        {
            $link = $this->sql_connect();
            $sql = @mysql_query($query,$link) or $this->Mysql_Die(mysql_error(),$query);
            $datos = array();
            while ($r = @mysql_fetch_assoc($sql))
            {
                $datos[]=$r;
            }
            @mysql_close($link);
            if ($debug)
            {
                echo "<pre>";
                echo $query;
                print_r($datos);
                echo "</pre>";
            }
            return $datos;
        }

        public function update($database,$table,$index,$data)
        {
            $link = $this->sql_connect();
            $query = "UPDATE $database.$table SET ";
            foreach ($data as $key=>$value)
            {
                $query .= "`$key`='".trim(mysql_real_escape_string($value))."',";
            }
            $query = substr($query,0,strlen($query)-1);
            $query .= " WHERE `id`={$index}";
            @mysql_query($query);
            @mysql_close($link);
        }

        public function insert($database,$table,$data)
        {
            $link = $this->sql_connect();
            $query = "INSERT INTO $database.$table (";
            foreach ($data as $key=>$value)
            {
                $query .= "`$key`,";
            }
            $query = substr($query,0,strlen($query)-1);
            $query .= ") VALUES (";

            foreach ($data as $key=>$value)
            {
                $query .= "'".trim(mysql_real_escape_string($value))."',";
            }
            $query = substr($query,0,strlen($query)-1) . ");";

            @mysql_query($query);
            $ID = mysql_insert_id();
            @mysql_close($link);
            return $ID;
        }

        public function get_record($database,$table,$index)
        {
            //Devuelte un registro
            $link = $this->sql_connect();
            $record = @mysql_fetch_assoc(@mysql_query("SELECT * FROM $database.$table WHERE id='$index'"));
            @mysql_close($link);
            return $record;
        }

        public function count_records($database,$table,$index_name,$index)
        {
            //Devuelte el número de registros encontrados para una condición
            $link = $this->sql_connect();
            $num = @mysql_result(@mysql_query("SELECT COUNT(*) FROM $database.$table WHERE `$index_name`='$index'"),0);
            @mysql_close($link);
            return $num;
        }

        public function sql_test()
        {
            echo "SQL test ok!";
        }

        private function Mysql_Die($mysql_error)
        {
            global $die_on_error;
            if ($die_on_error)
            {
                die($mysql_error);
            }
        }
    }
?>